package com.frank.work;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 小石潭记
 * @date 2021/12/21 10:49
 * @Description: 生产信息
 */
@Slf4j
@RestController
@RequestMapping("/producer")
@Api(value = "测试发送消息", tags = "发送消息")
public class ProducerController {

    @Autowired
    private PipelineManager pipelineManager;

    @Autowired
    private GroupTaskService groupTaskService;

    @GetMapping //http://localhost:8080/producer?id=1
    @ApiOperation(value = "发送消息", httpMethod = "GET")
    public void producer(@Param("id") int id) {
        BasicInfo basicInfo = new BasicInfo();
        basicInfo.setId(id);

        pipelineManager.getRootPipelineTasks().forEach(
                (key, root) -> {
                    // 判断是不是根节点
                    if (root.getNodeTask() instanceof GroupTaskMatch) {
                        // 在根节点可能需要一些判断，根据match的返回值判断是否需要继续执行，可能传过来的数据，
                        // 不是这次task需要执行的数据返回false则不执行，返回true则加入到队列进行处理
                        if (!((GroupTaskMatch) root.getNodeTask()).match(basicInfo)) {
                            return;
                        }
                        try {
                            groupTaskService.addTask(root, basicInfo);
                        } catch (Exception e) {
                            log.error("添加任务异常:{}", e.getMessage());
                        }
                    }
                }
        );
    }

}
